package tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class largestValues {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Deque<TreeNode> que = new LinkedList<>();
        if (root == null){
            return result;
        }
        que.offer(root);
        while (!que.isEmpty()){
            int len = que.size();
            List<Integer> temp = new ArrayList<>();
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < len; i++) {
                TreeNode item = que.pollFirst();
                if (item.val > max){
                    max = item.val;
                }
                if (item.left != null){
                    que.offerLast(item.left);
                }
                if (item.right != null){
                    que.offerLast(item.right);
                }
            }
            result.add(max);
        }
        return result;
    }
}
